package optimizers.tools;

//author: nicolas.bredeche(at)isir.upmc.fr
//created 2013-2-8

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileManager {

	File file;
	FileWriter fw;
	BufferedWriter bw;
	
	public FileManager ( String filename ) 
	{
		try {
				 
			file = new File(filename);
			if (!file.exists()) 
				file.createNewFile();
			fw = new FileWriter(file.getAbsoluteFile());
			bw = new BufferedWriter(fw);
		} catch (IOException e) {
			e.printStackTrace();
		}			
	}
	
	public void close()
	{
		try {
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	
	}

	public void write( String __str)
	{
		try {
			bw.write(__str);
			bw.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
	
	public static void main(String[] args) { // source : http://www.mkyong.com/java/how-to-write-to-file-in-java-bufferedwriter-example/
		try {
			 
			String content = "Testing FileMamager.";
 
			File file = new File("test.txt");
 
			// if file doesnt exists, then create it
			if (!file.exists()) {
				file.createNewFile();
			}
 
			FileWriter fw = new FileWriter(file.getAbsoluteFile());
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write(content);
			bw.close();
 
			System.out.println("Done");
 
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
